{extend name='../app/admin/view/common/pop.html' /}
{block name="content"}
<form class="layui-form j-form" style="margin-top: 15px" id="form">
    <div class="layui-form-item">
        <label for="" class="layui-form-label">标签名</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="title" value="{$info.title}">
        </div>
        <div class="layui-input-block j-form-tips">
            请输入标签名
        </div>
    </div>
    <div class="layui-form-item">
        <label for="" class="layui-form-label">图标</label>
        <div class="layui-input-block j-upload">
            <a href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm j-upload-dom">
                <input type="file" name="icon" id="upload_1" class="j-file" onchange="upload_icon(event,this)"
                       accept="image/*">
                <input type="text" class="j-upload-input" readonly="" value="{$info.icon_path}">
                <input type="hidden" class="j-upload-input" readonly="" value="{$info.icon}" name="iconfile">
                <i class="layui-icon layui-icon-upload"></i>选择文件</a>
        </div>
        <div class="j-img-preview-icon">
            {if $info.icon_path}
            <img src="{$info.icon_path}" alt="" class="j-img-preview-item" ondblclick="removePreviewImg(this)">
            {/if}
        </div>

        <div class="layui-input-block j-form-tips">请选择图标，双击可删除</div>
        <script>
            function upload_icon(event, e) {
                var file = event.target.files[0]
                    , preview = document.querySelector('.j-img-preview-icon')
                    , reader = new FileReader();
                if (!file) {
                    layui.layer.msg('请选择文件');
                } else {
                    $(e).next().val($(e).val())

                    reader.addEventListener("load", function () {
                        if (preview.children.length >= 1) {
                            preview.innerHTML = '';
                        }
                        var image = new Image();
                        image.title = file.name;
                        image.src = this.result;
                        image.classList.add('j-img-preview-item');
                        image.classList.add('j-img-dbclick-remove');
                        image.ondblclick = function (e) {
                            $(e.currentTarget).parent().prev().find('input[type=text]').val('')
                            $(e.currentTarget).parent().prev().find('input[type=hidden]').val('')
                            $(e.currentTarget).remove();
                        }

                        preview.append(image);
                    }, false);

                    reader.readAsDataURL(file);
                }
            }
        </script>
    </div>
    <div class="layui-form-item">
        <label for="" class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" lay-skin="switch" lay-filter="status" lay-text="显示|隐藏" {if $info.status == 1}checked{/if}>
        </div>
        <div class="layui-input-block j-form-tips">
            请选择状态
        </div>
    </div>
    <div class="layui-form-item">
        <label for="" class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" class="layui-input" name="sort" value="{$info.sort}">
        </div>
        <div class="layui-input-block j-form-tips">
            请输入排序值数值越小越靠前
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-form-label"></div>
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="create">提交</button>
        </div>
    </div>
</form>
{/block}

{block name="script"}
<script>
    layui.use(['layer', 'form', 'jquery'], function () {
        var layer = layui.layer,
            form = layui.form,
            $ = layui.jquery;
        form.render();

        form.on('submit(create)', function (data) {
            var fd = new FormData(document.getElementById("form"));
            console.log(fd);
            $.ajax({
                url: "{:plugin_url()}",
                type: 'post',
                processData: false,//必须
                contentType: false,//必须
                data: fd,
                success: function (r) {
                    if (r.code == 1) {
                        layer.msg(r.msg);
                    } else {
                        layer.msg(r.msg);
                    }
                },
                error: function (r) {
                    layer.msg(r.msg);
                }
            });
            return false;
        })
    })

</script>
{/block}